package com.longcai.piaojia.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.longcai.piaojia.BaseApplication;
import com.longcai.piaojia.R;
import com.longcai.piaojia.adapter.FeedbackDetailAdapter;
import com.longcai.piaojia.conn.FeedbackDetailListGet;
import com.longcai.piaojia.dialog.WarnningDialog;
import com.longcai.piaojia.util.MyToast;
import com.module.pull.PullToRefreshBase;
import com.module.pull.PullToRefreshListView;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilApp;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/30.
 */
public class FeedBackDetailActivity extends BaseActivity implements View.OnClickListener {

    @BoundView(R.id.feedback_detail_lv) private PullToRefreshListView pullToRefreshListView;
    @BoundView(R.id.feedback_detail_bottom_rl) private RelativeLayout feedback_detail_bottom_rl;

    private ListView feedback_detail_lv;

    private List<FeedbackDetailListGet.FeedbackDetail> lists = new ArrayList<>();

    private FeedbackDetailAdapter adapter;

    private FeedbackDetailListGet.FeedbackDetialEntity feedbackDetial;

    private FeedbackDetailListGet feedbackDetailListGet = new FeedbackDetailListGet(new AsyCallBack<FeedbackDetailListGet.FeedbackDetialEntity>() {

        @Override
        public void onSuccess(String toast, int type, FeedbackDetailListGet.FeedbackDetialEntity feedbackDetialEntity) throws Exception {

            feedbackDetial = feedbackDetialEntity;

            lists.addAll(feedbackDetialEntity.feedbackDetailList);

            adapter.notifyDataSetChanged();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            MyToast.show(context, toast, MyToast.LENGTH_SHORT);
        }

        @Override
        public void onEnd(String toast, int type) throws Exception {


            pullToRefreshListView.onPullUpRefreshComplete();

            pullToRefreshListView.onPullDownRefreshComplete();

        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback_detail);

        setBackTrue();

        setTitleName(getString(R.string.feedback_right_title_update_text));

        pullToRefreshListView.setScrollLoadEnabled(false);
        pullToRefreshListView.setPullLoadEnabled(true);
        pullToRefreshListView.setPullRefreshEnabled(true);

        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

                lists.clear();
                feedbackDetailListGet.page = 1;
                feedbackDetailListGet.execute(FeedBackDetailActivity.this, false);

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

                if (lists != null && feedbackDetial.total != lists.size()) {

                    feedbackDetailListGet.page = feedbackDetailListGet.page + 1;

                    feedbackDetailListGet.execute(FeedBackDetailActivity.this, false);

                } else {

                    pullToRefreshListView.onPullUpRefreshComplete();

                    pullToRefreshListView.onPullDownRefreshComplete();

                }

            }
        });

        feedback_detail_lv = pullToRefreshListView.getRefreshableView();

        feedback_detail_lv.setDivider(null);

        feedback_detail_lv.setAdapter(adapter = new FeedbackDetailAdapter(context, lists));


        feedbackDetailListGet.page = 1;
        feedbackDetailListGet.user_id = BaseApplication.BasePreferences.readUserId();
        feedbackDetailListGet.execute(this);

        feedback_detail_bottom_rl.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.feedback_detail_bottom_rl:

                new WarnningDialog(this, BaseApplication.BasePreferences.readCustomServiceTel(), getString(R.string.cancel_text), getString(R.string.confirm_text)) {
                    @Override
                    protected void onConfirm() {

                        UtilApp.call(FeedBackDetailActivity.this, BaseApplication.BasePreferences.readCustomServiceTel());

                    }
                }.show();

                break;

        }
    }



}
